//
//  AppDelegate.m
//  bird_cloud_artist
//
//  Created by 闫珍 on 17/1/4.
//  Copyright © 2017年 dianchuang. All rights reserved.
//

#import "AppDelegate.h"
#import "WelcomeViewController.h"

@interface AppDelegate ()<UITabBarDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor blackColor];
    
    
    if((UDfirstLaunch == nil) || (UDfirstLaunch != nil && ![UDfirstLaunch isEqualToString:Current_Version])) {//记录首次启动,启动动画
        
        [USER_DEFAULT setObject:Current_Version forKey:UDfirstLaunchIdentify];
        
        [USER_DEFAULT synchronize];
        
        [self _initTabBarView];
        
        WelcomeViewController *welcome = [[WelcomeViewController alloc] init];
        self.window.rootViewController = welcome;
        [self.window insertSubview:welcome.view atIndex:0];
        
        welcome.endEvent = ^(){
            //当要进入主程序
            self.window.rootViewController = _tabBarController;
        };
    }else {
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
        [self _initTabBarView];
        self.window.rootViewController = _tabBarController;
    }
    
    //window初始化
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)_initTabBarView
{
    //    NSMutableArray *tabItems = [[NSMutableArray alloc] init];
    _homeViewController = [[HomeViewController alloc] init];
    _homeViewController.title = @"首页";
    MLNavigationController *homeNav = [[MLNavigationController alloc] initWithRootViewController:_homeViewController];
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:_homeViewController.title
                                                        image:[UIImage imageNamed:@""]
                                                          tag:1001];
    homeNav.tabBarItem = item1;
    //    [tabItems addObject:item1];
    
    
    TaskViewController *findController = [[TaskViewController alloc]init];
    findController.title = @"任务";
    MLNavigationController *categoryNav = [[MLNavigationController alloc] initWithRootViewController:findController];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:findController.title
                                                        image:[UIImage imageNamed:@""]
                                                          tag:1002];
    
    categoryNav.tabBarItem = item2;
    
    
    _tabViewControllers = [[NSMutableArray alloc] initWithObjects:homeNav,categoryNav, nil];
    _tabBarController = [[UITabBarController alloc] init];
    _tabBarController.delegate = self;
    _tabBarController.viewControllers = _tabViewControllers;
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, APP_SCREEN_WIDTH, 49)];
    backView.backgroundColor = [UIColor whiteColor];
    [_tabBarController.tabBar insertSubview:backView atIndex:0];
    
    _rootViewControllers = @[_homeViewController,findController];
    _rootViewController = _rootViewControllers[0];
    
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -2)];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBCOLOR(139, 135, 135)}
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBCOLOR(255, 110, 155)}
                                             forState:UIControlStateSelected];
    
    NSInteger i = 1;
    for (UIViewController *controller in _tabBarController.viewControllers) {
        UITabBarItem *item = controller.tabBarItem;
        NSString *imageString = [NSString stringWithFormat:@"tabbar_icon_%ld", (long)i];
        item.image = [[UIImage imageNamed:imageString] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        NSString *selectedImageString = [NSString stringWithFormat:@"tabbar_icon_select_%ld", (long)i++];
        item.selectedImage = [[UIImage imageNamed:selectedImageString] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
}

#pragma mark - UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSInteger tag = viewController.tabBarItem.tag - 1000;
    if (tag < 1) {
        return;
    }
    
    _rootViewController = _rootViewControllers[tag - 1];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSInteger tag = viewController.tabBarItem.tag - 1000;
    
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
